package com.common.core.utils.imageUtils;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.CountDownTimer;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

/**
 * Created by lajin on 16/8/23.
 */
public class PopUtils {
    private PopupWindow pop;
    private int popupHeight;
    private int popupWidth;

    public final static int ON_PARENT_VIEW = 0;
    public final static int ON_PARENT_VIEW_LEFT = 1;
    public final static int ON_PARENT_VIEW_TOP = 2;
    public final static int ON_PARENT_VIEW_RIGHT = 3;
    public final static int ON_PARENT_VIEW_BOTTOM = 4;

    public void showPop(final Activity ac, int layoutId, int childViewId,
                        View parentView,
                        boolean outsideTouchable, View.OnClickListener listener){
        if (ac == null || ac.isFinishing()){
            return;
        }

        if (pop == null){
            View v = LayoutInflater.from(ac).inflate(layoutId, null);
            if (listener != null){
                View child = v.findViewById(childViewId);
                if (child != null) {
                    child.setOnClickListener(listener);
                }
            }
            v.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
            popupHeight = v.getMeasuredHeight();  //获取测量后的高度

            if (parentView == null) {
                pop = new PopupWindow(v, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            }else{
                pop = new PopupWindow(v, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            }
            pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {

                }
            });
            pop.setBackgroundDrawable(new BitmapDrawable());
            pop.setOutsideTouchable(outsideTouchable);
        }

        if (pop.isShowing()){

        }else {
            if (parentView != null){
                int[] location = new int[2];
                parentView.getLocationOnScreen(location);
                pop.showAtLocation(parentView, Gravity.NO_GRAVITY, location[0], location[1] - popupHeight);
            }else{
                pop.showAtLocation(ac.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
            }
        }
    }

    public boolean isShowing(){
        return pop != null && pop.isShowing();
    }

    public void disPop(){
        if (pop != null && pop.isShowing()){
            pop.dismiss();
        }
    }

    public void showPop(final Activity ac, int layoutId, int childViewId, View parentView, int gravity,
                        long countTime, int widthParam, int heightParam, int onParentViewLoaction,
                        boolean outsideTouchable, View.OnClickListener listener){
        if (ac == null || ac.isFinishing()){
            return;
        }

        if (pop == null){
            View v = LayoutInflater.from(ac).inflate(layoutId, null);
            if (listener != null){
                View child = v.findViewById(childViewId);
                if (child != null) {
                    child.setOnClickListener(listener);
                }
            }
            v.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
            popupHeight = v.getMeasuredHeight();  //获取测量后的高度
            popupWidth = v.getMeasuredWidth();

            pop = new PopupWindow(v, widthParam, heightParam);

            pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {

                }
            });
            pop.setBackgroundDrawable(new BitmapDrawable());
            pop.setOutsideTouchable(outsideTouchable);
        }

        if (pop.isShowing()){

        }else {
            if (ac == null || ac.isFinishing()){
                return;
            }

            if (parentView != null){
                int[] location = new int[2];
                parentView.getLocationOnScreen(location);
                if (onParentViewLoaction == ON_PARENT_VIEW_LEFT){
                    pop.showAtLocation(parentView, gravity, location[0] - popupWidth, location[1]);
                }else if (onParentViewLoaction == ON_PARENT_VIEW_TOP){
                    pop.showAtLocation(parentView, gravity, location[0], location[1] - popupHeight);
                }else if (onParentViewLoaction == ON_PARENT_VIEW_RIGHT){
                    pop.showAtLocation(parentView, gravity, location[0] * 2, location[1]);
                }else if (onParentViewLoaction == ON_PARENT_VIEW_BOTTOM){
                    pop.showAtLocation(parentView, gravity, location[0], location[1] * 2);
                }else{
                    pop.showAtLocation(parentView, gravity, location[0], location[1]);
                }
            }else{
                pop.showAtLocation(ac.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
            }
            startTimer(ac, countTime);
        }
    }

    private CountDownTimer cdt;
    private void startTimer(final Activity ac, long countTime){
        if (countTime <= 0){
            return;
        }

        stopTimer();

        if (cdt == null){
            cdt = new CountDownTimer(countTime, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {

                }

                @Override
                public void onFinish() {
                    if (ac == null || ac.isFinishing()){

                    }else {
                        disPop();
                    }
                    cdt = null;
                }
            };
        }

        cdt.start();
    }

    private void stopTimer(){
        if (cdt != null){
            cdt.cancel();
            cdt = null;
        }
    }
}
